package com.next.muscu.visual;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.swing.AbstractAction;
import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import com.next.muscu.Application;
import com.next.muscu.data.ExoType;
import com.next.muscu.data.GenericListModel;
import com.next.muscu.data.ObservableList;

public class VisualExoType extends JPanel implements ListSelectionListener {

	JList list = new JList();
	JButton okBt = new JButton("ok");
	JButton newBt = new JButton("nouveau");
	JButton deleteBt = new JButton("effacer");
	ObservableList<ExoType> data;
	VisualExoTypeSimplePanel dataPanel;
	
	VisualExoType()
	{
		data = new ObservableList<ExoType> ();
		data.add(new ExoType(0,"1","test1"));
		data.add(new ExoType(1,"2","test2"));
		data.add(new ExoType(2,"3","test3"));
		Collections.sort(data);
		buildGui();
		this.setVisible(true);
	}
	
	private void buildGui()
	{
		this.setLayout(new BorderLayout());
		list.setModel(new GenericListModel<ExoType>(data));
		list.setCellRenderer(new ExoTypeJlistCellRender());
		list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION );
		list.addListSelectionListener(this);
        this.add(list,BorderLayout.CENTER);
		JPanel panelBas = new JPanel();
		panelBas.setLayout(new BoxLayout(panelBas,BoxLayout.X_AXIS));
		dataPanel = new VisualExoTypeSimplePanel();
		panelBas.add(dataPanel);
		okBt.setAction(new BtOkAction());
		okBt.setIcon(Application.getAppl().getImage().get(Application.getAppl().IMG_ACCEPT));
		panelBas.add(okBt);
		JPanel upPanel = new JPanel();
		upPanel.setLayout(new BoxLayout(upPanel,BoxLayout.X_AXIS) );
		newBt.setAction(new btNewAction());
		newBt.setIcon(Application.getAppl().getImage().get(Application.getAppl().IMG_ADD));
		upPanel.add(newBt,BorderLayout.NORTH);
		deleteBt.setAction(new btDeleteAction());
		deleteBt.setIcon(Application.getAppl().getImage().get(Application.getAppl().IMG_REMOVE));
		upPanel.add(deleteBt,BorderLayout.NORTH);
		this.add(upPanel,BorderLayout.NORTH);
		this.add(panelBas,BorderLayout.SOUTH);
	}

	public void valueChanged(ListSelectionEvent e) {	
		if(e.getValueIsAdjusting()){
		System.out.println((ExoType)list.getSelectedValue());
		dataPanel.setData((ExoType)list.getSelectedValue());
		}
	}
	private class btDeleteAction extends AbstractAction
	{

		public void actionPerformed(ActionEvent arg0) {
			ExoType e = (ExoType) list.getModel().getElementAt(list.getSelectedIndex());
			data.remove(e);
		}
		
	}
	
	private class BtOkAction extends AbstractAction
	{
		public void actionPerformed(ActionEvent arg0) {
			int i = data.indexOf(dataPanel.getData());
			data.get(i).setName(dataPanel.getData().getName());
		}
		
	}
	
	private class btNewAction extends AbstractAction
	{

		public void actionPerformed(ActionEvent e) {
			Collections.sort(data);
			ExoType e1 =data.get(data.size()-1);
			data.add(new ExoType(e1.getId()+1,Integer.toString((Integer.parseInt(e1.getCode()))+1),"nouveau"));	
		}
		
	}
	
}
